<?php

namespace Osp\CategoryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Osp\CategoryBundle\Entity\Category;
use JMS\SecurityExtraBundle\Annotation\Secure; 

class CategoryController extends Controller
{
	/**
   	* @Secure(roles="ROLE_ADMIN")
   	*/
    public function categoryManagementAction()
    {

    	$error = null;
    	//Récupération des utilisateurs dans la base
	   	$repository = $this->getDoctrine()->getManager()->getRepository('OspCategoryBundle:Category');
		$liste_categories = $repository->findAll();

		if (!$liste_categories) 
		{ 
			//throw $this->createNotFoundException('No categories in the db');
			$error = "Aucune catégorie dans la Base de données";
		}


		//Création du formulaire
		$category = new Category();

		// On crée le FormBuilder grâce à la méthode du contrôleur
		$formBuilder = $this->createFormBuilder($category);

		$formBuilder
		->add('libelle', 'text');

		$form = $formBuilder->getForm();

		//On récupère la requête
		$request = $this->get('request');


		//Si la requête est de type POST
		if($request->getMethod() == 'POST')
		{
			//category contient maintenant les valeurs du formulaire
			$form->bind($request);
			
			if($form->isValid())
			{
				$entityManager = $this->getDoctrine()->getManager();
				$entityManager->persist($category);
				$entityManager->flush();

				return $this->redirect($this->generateUrl('osp_category_homepage'));
			}

				else
				{
					return $this->render('OspCategoryBundle:Category:errors.html.twig', array('error' => $form->getErrorsAsString()));
				}

		}

		else
		{
        	return $this->render('OspCategoryBundle:Category:categoryManagement.html.twig', array('categories' => $liste_categories, 'form' => $form->createView(), 'error' => $error));
    	}
    }


    /**
   	* @Secure(roles="ROLE_ADMIN")
   	*/
    public function deleteCategoryAction($id) 
	{ 
		$entityManager = $this->getDoctrine()->getManager(); 
		$category = $entityManager->getRepository('OspCategoryBundle:Category')->find($id); 

		if (!$category) 
			{ 
				throw $this->createNotFoundException('No category found for id '.$id); 
			} 

		$entityManager->remove($category); 
		$entityManager->flush(); 

		return $this->redirect($this->generateUrl('osp_category_homepage')); 
	}

	/**
   	* @Secure(roles="ROLE_ADMIN")
   	*/
	public function updateCategoryAction($id)
	{ 
		$entityManager = $this->getDoctrine()->getManager(); 
		$category = $entityManager->getRepository('OspCategoryBundle:Category')->find($id); 

		if (!$category) 
			{ 
				throw $this->createNotFoundException('No category found for id '.$id);
			}


		// On crée le FormBuilder grâce à la méthode du contrôleur
		$formBuilder = $this->createFormBuilder($category);

		$formBuilder
		->add('libelle', 'text');

		$form = $formBuilder->getForm();

		//On récupère la requête
		$request = $this->get('request');


		//Si la requête est de type POST
		if($request->getMethod() == 'POST')
		{
			//category contient maintenant les valeurs du formulaire
			$form->bind($request);
			
			if($form->isValid())
			{
				$entityManager = $this->getDoctrine()->getManager();
				$entityManager->flush();

				return $this->redirect($this->generateUrl('osp_category_homepage'));
			}

				else
				{
					return $this->render('OspCategoryBundle:Category:errors.html.twig', array('error' => $form->getErrorsAsString()));
				}

		}	 

		return $this->render('OspCategoryBundle:Category:categoryUpdate.html.twig', array('category' => $category, 'form' => $form->createView())); 
	}


}
